//
//  HLTLoginTableViewController.m
//  huiliantong
//
//  Created by hlt on 15/8/28.
//  Copyright (c) 2015年 hlt. All rights reserved.
//

#import "HLTLoginTableViewController.h"
//#import "HLTloginTableViewCell.h"
//#import "HLTzhuceTableViewCell.h"
#import "HLTzhuceTableViewController.h"
#import "HLTWangjipasswordTableViewController.h"
@interface HLTLoginTableViewController ()

@end

@implementation HLTLoginTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//     [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"01登录界面2_r2_c1.gif"] forBarMetrics:UIBarMetricsDefault];
    //设置返回的按钮的字体的颜色的
    self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
    //设置导航的背景颜色的
    self.navigationController.navigationBar.barTintColor = navigationBarColor;
    
    
//    self.navigationController.title=@"登陆";
    [self.navigationItem setTitle:@"登陆"];
    
    //_yinchang为yes的时候就不隐藏
    if (_yinchang) {
        
        
    }else
    {
        //隐藏返回按钮
        [self.navigationItem.backBarButtonItem setTitle:@" "];
        [self.navigationItem  setHidesBackButton:YES];
        
    }
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    [self.password addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    
    
}



- (void)textFieldDidChange:(UITextField *)textField
{
    if (textField == self.password) {
        if (textField.text.length > 6) {
            textField.text = [textField.text substringToIndex:6];
        }
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//#pragma mark - Table view data source
//
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    // Return the number of sections.
//    return 1;
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    // Return the number of rows in the section.
//    return 1;
//}
//
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    static NSString *identify1 = @"HLTloginTableViewCell";
//
//    
//    HLTloginTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify1];
//
//    if(cell == nil)
//    {
//        cell= [[[NSBundle mainBundle]loadNibNamed:@"HLTloginTableViewCell" owner:nil options:nil] firstObject];
//       
//    }
////    cell.backgroundColor=[UIColor clearColor];//关键语句
//    cell.selectionStyle=UITableViewCellSelectionStyleNone;
//    cell.parent=self;
//
//    return cell;
//}
//
//
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    
//        return screen_height-64-49;
//    
//}


- (IBAction)zhuce:(id)sender {
    //取消键盘
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    HLTzhuceTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTzhuceTableViewController"];
    
    [self.navigationController pushViewController:vc animated:NO];
    
    
    
}


- (IBAction)wangjipassword:(id)sender {
    
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    
    
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    HLTWangjipasswordTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTWangjipasswordTableViewController"];
    
    
    [self.navigationController pushViewController:vc animated:NO];
    
    
    
}


- (IBAction)login:(id)sender {
    
    
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    
    UIView *view1=[[UIView alloc] initWithFrame:self.view.bounds];
    view1.backgroundColor=[UIColor grayColor];
    view1.alpha=0.4;
    [self.view addSubview:view1];
    MONActivityIndicatorView *indicatorView = [[MONActivityIndicatorView alloc] init];
    indicatorView.delegate = self;
    indicatorView.numberOfCircles = 3;
    indicatorView.radius = 20;
    indicatorView.internalSpacing = 3;
    indicatorView.center = self.view.center;
    [indicatorView startAnimating];
    [self.view addSubview:indicatorView];
    
    
    
    
    //base64 编码
      NSString *base64pwd= [SetBase64andMd5 base64:self.password.text ncryptOrDecrypt:kCCEncrypt];
    
    NSString *url=[loginurl stringByAppendingFormat:@"&username=%@&password=%@",self.tel.text,base64pwd];
    [HLTNetWorkingtool getyouhuilist:url success:^(NSMutableArray *ary) {
        
        //        NSArray *aaary=ary;
        NSDictionary *dic=ary[0];
        NSLog(@"%@",dic[@"bind_card"]);
        if ([dic[@"status"] isEqual:@"0"]) {
            
            
            if ([dic[@"user_rank"] isEqual:@"4"]) {
                
                [indicatorView stopAnimating];
                [view1 removeFromSuperview];
                
                UIAlertView *alert=[[UIAlertView alloc ]initWithTitle:@"提示" message:@"对不起，您的账号是汇联通商户权限，目前该版本不支持商户登陆" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
                [alert show];
                
            }
            else
            {
            
            if ([dic[@"bind_card"] isEqual:@"0"]||[dic[@"bind_card"] isEqual:@"00"]) {
                Account *acc=[[AccountTool sharedAccountTool] currentAccount];
                acc.pwd=self.password.text;
                acc.custPhone=self.tel.text;
                acc.custFlag=dic[@"user_rank"];
                acc.cardNo=@"";
                [[AccountTool sharedAccountTool] addAccounts:acc];
                
                
                
                
                
                //查询个人资料
                NSString *url=[chaxunuserinfo stringByAppendingFormat:@"&username=%@",acc.custPhone];
                [HLTNetWorkingtool getary:url success:^(NSArray *ary) {
                    
                    
                    
                    acc.Email=ary[0][@"email"];
                    
                    
                    acc.custName=ary[0][@"name"];
                    
                    acc.custIdent=ary[0][@"id_card"];
                    
                    acc.custSex=ary[0][@"sex"];
                    [[AccountTool sharedAccountTool] addAccounts:acc];
                    
                    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
                    
//                    [self.navigationController popViewControllerAnimated:YES];
                    [indicatorView stopAnimating];
                    [view1 removeFromSuperview];
                    
                } faliure:^(NSString *error) {
                    
                    
                    
//                     [self.navigationController popViewControllerAnimated:YES];
                    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
                    [indicatorView stopAnimating];
                    [view1 removeFromSuperview];
                    
                }];
                
                
                
                
            }
            else
            {
                
                
                //查询余额
                NSString *url=[chaxunyue stringByAppendingFormat:@"&user_name=%@&cardNum=%@",self.tel.text,dic[@"bind_card"]];
                [HLTNetWorkingtool getdic:url success:^(NSDictionary *ary) {
                    //将nsnumber 装换成nsstring
                    NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
                    NSString *str = [numberFormatter stringFromNumber:ary[@"code"]];
                    
                    if ([str isEqual:@"2"]) {
                        Account *acc=[[AccountTool sharedAccountTool] currentAccount];
                        acc.pwd=self.password.text;
                        acc.cardNo=dic[@"bind_card"];
                        acc.custFlag=dic[@"user_rank"];
                        acc.custPhone=self.tel.text;
                        acc.YKTCardBalance=ary[@"deposit"];
                        [[AccountTool sharedAccountTool] addAccounts:acc];
                        NSLog(@"%@",ary);
                        //查询个人资料
                        NSString *url1=[chaxunuserinfo stringByAppendingFormat:@"&username=%@",acc.custPhone];
                        [HLTNetWorkingtool getary:url1 success:^(NSArray *ary) {
                            
                            
                            
                            acc.Email=ary[0][@"email"];
                            
                            
                            acc.custName=ary[0][@"name"];
                            
                            acc.custIdent=ary[0][@"id_card"];
                            
                            acc.custSex=ary[0][@"sex"];
                            [[AccountTool sharedAccountTool] addAccounts:acc];
//                            [self.navigationController popViewControllerAnimated:YES];
                            [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
                            [indicatorView stopAnimating];
                            [view1 removeFromSuperview];
                            
                        } faliure:^(NSString *error) {
                            
                            [indicatorView stopAnimating];
                            [view1 removeFromSuperview];
                            
                        }];

                        
                        
                        
                        
                    }else
                    {
                        
                        UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"提示" message:ary[@"msg"] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
                        [alert show];
//                        [self.navigationController popViewControllerAnimated:YES];
                        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
                        [indicatorView stopAnimating];
                        [view1 removeFromSuperview];
                        
                    }
                } faliure:^(NSString *error) {
                    [indicatorView stopAnimating];
                    [view1 removeFromSuperview];
                    
                }];
                
                
                
                
                
                
                
            }
            
            
            }
        }else
        {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"用户名或者密码错误，请重新输入"   message:nil  delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
            [alert show];
            [indicatorView stopAnimating];
            [view1 removeFromSuperview];
        }
        
    } faliure:^(NSString *error) {
        [indicatorView stopAnimating];
        [view1 removeFromSuperview];
        
    }];
    
    
    
    
    
    
}





- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    
}





- (void)viewWillAppear:(BOOL)animated
{
    //清空登陆信息
    Account *acc=[[AccountTool sharedAccountTool] currentAccount];
    
    [[AccountTool sharedAccountTool] tuichu:acc];
    
    
}



#pragma mark - MONActivityIndicatorViewDelegate Methods

- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView
      circleBackgroundColorAtIndex:(NSUInteger)index {
    CGFloat red   = (arc4random() % 256)/255.0;
    CGFloat green = (arc4random() % 256)/255.0;
    CGFloat blue  = (arc4random() % 256)/255.0;
    CGFloat alpha = 1.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}






@end
